//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.sitanyun.merchant.guide.weiht;

import android.annotation.SuppressLint;
import android.os.Build.VERSION;
import android.text.TextUtils;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Provider;
import java.security.SecureRandom;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

import androidx.annotation.RequiresApi;

public class AESUtils {
    private static final String SHA1_PRNG = "SHA1PRNG";
    private static final int KEY_SIZE = 30;

    public AESUtils() {
    }

    @SuppressLint({"DeletedProvider", "GetInstance"})
    public static String des(String content, String password,  int type) {
        if (!TextUtils.isEmpty(content) && !TextUtils.isEmpty(password)) {
            try {
                SecretKeySpec secretKeySpec;
                if (VERSION.SDK_INT >= 28) {
                    secretKeySpec = deriveKeyInsecurely(password);
                } else {
                    secretKeySpec = fixSmallVersion(password);
                }

                Cipher cipher = Cipher.getInstance("AES");
                cipher.init(type, secretKeySpec);
                byte[] byteContent;
                if (type == 1) {
                    byteContent = content.getBytes("utf-8");
                    return parseByte2HexStr(cipher.doFinal(byteContent));
                } else {
                    byteContent = parseHexStr2Byte(content);
                    return new String(cipher.doFinal(byteContent), "utf-8");
                }
            } catch (BadPaddingException | IllegalBlockSizeException | UnsupportedEncodingException | InvalidKeyException | NoSuchPaddingException | NoSuchProviderException | NoSuchAlgorithmException var6) {
                var6.printStackTrace();
                return null;
            }
        } else {
            return null;
        }
    }

    @SuppressLint({"DeletedProvider"})
    private static SecretKeySpec fixSmallVersion(String password) throws NoSuchAlgorithmException, NoSuchProviderException {
        KeyGenerator generator = KeyGenerator.getInstance("AES");
        SecureRandom secureRandom;
        if (VERSION.SDK_INT >= 24) {
            secureRandom = SecureRandom.getInstance("SHA1PRNG", new CryptoProvider());
        } else {
            secureRandom = SecureRandom.getInstance("SHA1PRNG", "Crypto");
        }

        secureRandom.setSeed(password.getBytes());
        generator.init(128, secureRandom);
        byte[] enCodeFormat = generator.generateKey().getEncoded();
        return new SecretKeySpec(enCodeFormat, "AES");
    }

    @RequiresApi(
        api = 19
    )
    private static SecretKeySpec deriveKeyInsecurely(String password) {
        byte[] passwordBytes = password.getBytes(StandardCharsets.US_ASCII);
        return new SecretKeySpec(InsecureSHA1PRNGKeyDerivator.deriveInsecureKey(passwordBytes, 32), "AES");
    }

    private static String parseByte2HexStr(byte[] buf) {
        StringBuilder sb = new StringBuilder();
        byte[] var2 = buf;
        int var3 = buf.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            byte b = var2[var4];
            String hex = Integer.toHexString(b & 255);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }

            sb.append(hex.toUpperCase());
        }

        return sb.toString();
    }

    private static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1) {
            return null;
        } else {
            byte[] result = new byte[hexStr.length() / 2];

            for(int i = 0; i < hexStr.length() / 2; ++i) {
                int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
                int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
                result[i] = (byte)(high * 16 + low);
            }

            return result;
        }
    }

    private static final class CryptoProvider extends Provider {
        CryptoProvider() {
            super("Crypto", 1.0D, "HARMONY (SHA1 digest; SecureRandom; SHA1withDSA signature)");
            this.put("SecureRandom.SHA1PRNG", "org.apache.harmony.security.provider.crypto.SHA1PRNG_SecureRandomImpl");
            this.put("SecureRandom.SHA1PRNG ImplementedIn", "Software");
        }
    }
}
